home *** CD-ROM | disk | FTP | other *** search
- 10 'STRINGS.BAS - Demonstration program for working with strings
- 20 'From the GW-BASIC Tutorial Series (GWBT05, 10/15/1990)
- 30 '
- 40 'In this program we will use the following variables:
- 50 '
- 60 ' INPUTSTRING$ will be the string the user enters
- 70 ' LENGTH will be the length of the string we are working with
- 80 '! OUTPUT$ will be the part of the string we will work with
- 90 '
- 100 'Since it is possible to generate a LOT of errors if you ask for an invalid
- 110 'portion of a string (more characters than are in it, for example), we will
- 120 'check for errors BEFORE we ask BASIC to give us the requested part of the
- 130 'string...
- 140 '
- 150 'Start of main program:
- 160 KEY OFF
- 170 CLS
- 180 LOCATE 3,1,0
- 190 PRINT "STRINGS.BAS - Demonstration program for using strings in BASIC"
- 200 PRINT
- 210 PRINT "Before we can start, I need you to enter a string to work with. You"
- 220 PRINT "may enter anything you want: Words, numbers, sentences, and so on..."
- 230 PRINT "and I will convert whatever you put in into a string variable. You"
- 240 PRINT "may press ENTER alone to quit..."
- 250 PRINT
- 260 LINE INPUT "What is your string => ";INPUTSTRING$
- 270 '
- 280 'To check for errors, we need to know the total length of the string, so
- 290 'we use the LEN command
- 300 '
- 310 LENGTH=LEN(INPUTSTRING$)
- 320 '
- 330 'If the LENGTH is zero, this means that the user did NOT put in a string,
- 340 'but just pressed enter, so we quit if LENGTH is zero...
- 350 '
- 360 IF LENGTH = 0 THEN END
- 370 '
- 380 'If the length is greater than zero, we continue...
- 390 PRINT
- 400 PRINT "Demonstrating LEFT$"
- 410 INPUT "How many characters should I show you from the LEFT SIDE";LEFTLENGTH
- 420 '
- 430 'If you ask for more characters than are there, this can generate an error,
- 440 'so let's check to be sure the request is OK...
- 450 '
- 460 IF LEFTLENGTH < LENGTH THEN GOTO 500
- 470 BEEP
- 480 PRINT "You asked for too many characters! Only ";LENGTH;" are there!"
- 490 GOTO 400
- 500 OUTPUT$=LEFT$(INPUTSTRING$,LEFTLENGTH)
- 510 PRINT "The LEFT part you asked for is: ";OUTPUT$
- 520 PRINT
- 530 PRINT "Demonstrating RIGHT$"
- 540 INPUT "How many characters should I show you from the RIGHT SIDE";RIGHTLENGTH
- 550 '
- 560 'Again, if you ask for more than are there, this could be an error...
- 570 IF RIGHTLENGTH < LENGTH THEN GOTO 610
- 580 BEEP
- 590 PRINT "You asked for too many characters! Only ";LENGTH;" are there!"
- 600 GOTO 530
- 610 OUTPUT$=RIGHT$(INPUTSTRING$,RIGHTLENGTH)
- 620 PRINT "The RIGHT part you asked for is: ";OUTPUT$
- 630 PRINT
- 640 PRINT "Demonstrating MID$"
- 650 INPUT "Where should I start giving you characters from the MIDDLE";START
- 660 INPUT "And how many characters would you like to see";MIDDLELENGTH
- 670 '
- 680 'Now, we need to check two things: First, that the starting position is
- 690 'not greater than the total length of the string; and second, that the
- 700 'total number of characters needed is less than the total length of the
- 710 'string...
- 720 '
- 730 IF START < LENGTH THEN GOTO 770
- 740 BEEP
- 750 PRINT "Your starting position is too large!"
- 760 GOTO 650
- 770 IF START + MIDDLELENGTH < LENGTH THEN GOTO 810
- 780 BEEP
- 790 PRINT "You asked for more characters than are in the string!"
- 800 GOTO 650
- 810 OUTPUT$=MID$(INPUTSTRING$,START,MIDDLELENGTH)
- 820 PRINT "The MIDDLE part you asked for is: ";OUTPUT$
- 830 PRINT "Press any key to continue..."
- 840 WHILE INKEY$="":WEND
- 850 RUN
- 860 'End of program - STRINGS.BAS